package com.uetoken.cn.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.AmountCardAdapter;
import com.uetoken.cn.adapter.WalletStripAdapter;
import com.uetoken.cn.adapter.WalletsAdapter;
import com.uetoken.cn.adapter.model.AmountCardModel;
import com.uetoken.cn.adapter.model.WalletStripModel;
import com.uetoken.cn.adapter.model.WalletsModel;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.manager.UEDataManager;
import com.uetoken.cn.view.pagerecyclerview.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StockAccountsActivity extends BaseActivity {

    @BindView(R.id.rootFundInfoStockLayout)
    FrameLayout mRootFundInfoStockLayout;
    @BindView(R.id.selectMoneyTv)
    TextView selectMoneyTv;
    @BindView(R.id.cardRecyclerView)
    RecyclerView mCardRecyclerView;
    @BindView(R.id.customSumTv)
    TextView customSumTv;
    @BindView(R.id.customSumEt)
    EditText customSumEt;
    @BindView(R.id.customSumBtn)
    Button customSumBtn;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.customSumDetailsTv)
    TextView customSumDetailsTv;
    @BindView(R.id.customSumLayout)
    RelativeLayout customSumLayout;
    @BindView(R.id.surplusQuotaTv)
    TextView surplusQuotaTv;
    @BindView(R.id.thisWeekRemainQuotaTv)
    TextView thisWeekRemainQuotaTv;
    @BindView(R.id.nextWeekRemainQuotaTv)
    TextView nextWeekRemainQuotaTv;
    @BindView(R.id.remainQuotaLayout)
    LinearLayout remainQuotaLayout;
    @BindView(R.id.annotationTv)
    TextView annotationTv;
    @BindView(R.id.detailsTv)
    TextView detailsTv;
    @BindView(R.id.detailsLayout)
    RelativeLayout detailsLayout;
    @BindView(R.id.surplusQuotaLayout)
    RelativeLayout surplusQuotaLayout;
    @BindView(R.id.topUpSumLayout)
    RelativeLayout topUpSumLayout;
    @BindView(R.id.selectStockWayTv)
    TextView selectStockWayTv;
    @BindView(R.id.ueWalletBtn)
    TextView mUeWalletBtn;
    @BindView(R.id.dignityBtn)
    TextView mDignityBtn;
    @BindView(R.id.walletLayout)
    RelativeLayout walletLayout;
    @BindView(R.id.wayRecyclerView)
    RecyclerView mWayRecyclerView;
    @BindView(R.id.walletRecyclerView)
    RecyclerView mWalletRecyclerView;
    @BindView(R.id.selectWalletLayout)
    RelativeLayout selectWalletLayout;
    @BindView(R.id.topUpCodeTypeTv)
    TextView topUpCodeTypeTv;
    @BindView(R.id.svcTv)
    TextView svcTv;
    @BindView(R.id.svcDetailsTv)
    TextView svcDetailsTv;
    @BindView(R.id.svcIv)
    ImageView svcIv;
    @BindView(R.id.svcLayout)
    RelativeLayout svcLayout;
    @BindView(R.id.stockWayLayout)
    RelativeLayout stockWayLayout;
    @BindView(R.id.clickBtn1)
    Button clickBtn1;
    @BindView(R.id.clickBtn2)
    Button clickBtn2;
    @BindView(R.id.clickBtnLayout)
    RelativeLayout clickBtnLayout;
    @BindView(R.id.topbar)
    QMUITopBar mTopbar;

    //    @BindView(R.id.walletTypeLayout)
//    RelativeLayout walletTypeLayout;
//    @BindView(R.id.btcLayout)
//    RelativeLayout btcLayout;
//    @BindView(R.id.btcTv)
//    TextView btcTv;
//    @BindView(R.id.btcDetailsTv)
//    TextView btcDetailsTv;
//    @BindView(R.id.btcIv)
//    ImageView btcIv;
//    @BindView(R.id.usdtTv)
//    TextView usdtTv;
//    @BindView(R.id.usdtDetailsTv)
//    TextView usdtDetailsTv;
//    @BindView(R.id.usdtIv)
//    ImageView usdtIv;
//    @BindView(R.id.usdtLayout)
//    RelativeLayout usdtLayout;
//    @BindView(R.id.cnvLayout)
//    RelativeLayout cnvLayout;
//    @BindView(R.id.cnvTv)
//    TextView cnvTv;
//    @BindView(R.id.cnvDetailsTv)
//    TextView cnvDetailsTv;
//    @BindView(R.id.cnvIv)
//    ImageView cnvIv;

    private WalletsAdapter mWalletsAdapter;
    private WalletStripAdapter mWalletStripAdapter;
    private AmountCardAdapter mAmountCardAdapter;
    private View mView1;
    private List<AmountCardModel> denominationList;
    private List<WalletsModel> mWalletsModelList = new ArrayList<>();
    private List<WalletStripModel> mWalletStripModelList = new ArrayList<>();
    private boolean isUEWallet = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_stock_layout;
    }

    @Override
    public void initView() {
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }
        ButterKnife.bind(this);
        initTopBar();
        mView1 = LayoutInflater.from(this).inflate(R.layout.item_fund_info, null);
        mRootFundInfoStockLayout.addView(mView1);
        addCardLayout();
        initWallet();
        addWalletsLayout();
        addWalletStripLayout();
    }

    @Override
    public void initData() {
        getAmountCard();
        mAmountCardAdapter = new AmountCardAdapter();
        mCardRecyclerView.setAdapter(mAmountCardAdapter);
        mAmountCardAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AmountCardModel model = denominationList.get(position);
                ToastUtils.showShort(model.getPresented());
                LogUtils.dTag("mAmountCardAdapter", "getDenomination = " + model.getDenomination() + " getPresented = " + model.getPresented());
            }
        });
        getWalletData();
        mWalletsAdapter = new WalletsAdapter();
        mWayRecyclerView.setAdapter(mWalletsAdapter);
        mWalletsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                for (int i = 0; i < mWalletsModelList.size(); i++) {
                    WalletsModel mWalletsModel = mWalletsModelList.get(i);
                    boolean isSelected = false;
                    if (i == position) {
                        isSelected = true;
                    } else {
                        isSelected = false;
                    }
                    mWalletsModel.setSelected(isSelected);
                }
                adapter.notifyDataSetChanged();
            }
        });
        getWalletStripData();
        mWalletStripAdapter = new WalletStripAdapter(this);
        mWalletRecyclerView.setAdapter(mWalletStripAdapter);
        mWalletStripAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                for (int i = 0; i < mWalletStripModelList.size(); i++) {
                    WalletStripModel mWalletStripModel = mWalletStripModelList.get(i);
                    boolean isSelected = false;
                    if (i == position) {
                        isSelected = true;
                    } else {
                        isSelected = false;
                    }
                    mWalletStripModel.setSelected(isSelected);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void init() {

    }

    @OnClick({R.id.ueWalletBtn, R.id.dignityBtn, R.id.clickBtn1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ueWalletBtn:
                isUEWallet = true;
                initWallet();
                getWalletData();
                mWalletsAdapter.notifyDataSetChanged();
                getWalletStripData();
                mWalletStripAdapter.notifyDataSetChanged();
                break;
            case R.id.dignityBtn:
                isUEWallet = false;
                initWallet();
                getWalletData();
                mWalletsAdapter.notifyDataSetChanged();
                getWalletStripData();
                mWalletStripAdapter.notifyDataSetChanged();
                break;

            case R.id.clickBtn1:
                Intent intent = new Intent(this, PurchaseDetailActivity.class);
                startActivity(intent);
                break;

        }
    }

    private void initTopBar() {
        mTopbar.setTitle(R.string.stock_title_txt);
        mTopbar.addLeftImageButton(R.drawable.ic_back, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void addCardLayout() {
        //设置布局管理器 , 将布局设置成纵向
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 3);
        mCardRecyclerView.setLayoutManager(mGridLayoutManager);
        //设置分隔线
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
        mCardRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels, spacingInPixels, spacingInPixels, spacingInPixels));
    }

    private void initWallet() {
        int spacingHeight = getResources().getDimensionPixelSize(R.dimen.album_dp_30);
        RelativeLayout.LayoutParams linearParams1 = (RelativeLayout.LayoutParams) mUeWalletBtn.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        RelativeLayout.LayoutParams linearParams2 = (RelativeLayout.LayoutParams) mDignityBtn.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        if (isUEWallet) {
            mUeWalletBtn.setBackgroundResource(R.drawable.border_circular_bead_shape_2);
            mDignityBtn.setBackgroundResource(R.drawable.blue_circular_bead_shape_2);

            mUeWalletBtn.setTextColor(ContextCompat.getColor(this, R.color.color_app_theme));
            mDignityBtn.setTextColor(ContextCompat.getColor(this, R.color.durban_White));

            linearParams1.height = ViewGroup.LayoutParams.MATCH_PARENT;// 控件的宽强制设成30
            mUeWalletBtn.setLayoutParams(linearParams1); //使设置好的布局参数应用到控件

            linearParams2.height = spacingHeight;// 控件的宽强制设成30
            mDignityBtn.setLayoutParams(linearParams2); //使设置好的布局参数应用到控件
        } else {
            mUeWalletBtn.setBackgroundResource(R.drawable.blue_circular_bead_shape_2);
            mDignityBtn.setBackgroundResource(R.drawable.border_circular_bead_shape_2);

            mUeWalletBtn.setTextColor(ContextCompat.getColor(this, R.color.durban_White));
            mDignityBtn.setTextColor(ContextCompat.getColor(this, R.color.color_app_theme));

            linearParams1.height = spacingHeight;// 控件的宽强制设成30
            mUeWalletBtn.setLayoutParams(linearParams1); //使设置好的布局参数应用到控件

            linearParams2.height = ViewGroup.LayoutParams.MATCH_PARENT;// 控件的宽强制设成30
//            linearParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            mDignityBtn.setLayoutParams(linearParams2); //使设置好的布局参数应用到控件
        }
    }


    private void addWalletsLayout() {
        //设置布局管理器 , 将布局设置成纵向
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2);
        mWayRecyclerView.setLayoutManager(mGridLayoutManager);
        //设置分隔线
        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
        mWayRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels, spacingInPixels, spacingInPixels, spacingInPixels));
    }

    private void addWalletStripLayout() {
        //设置布局管理器 , 将布局设置成纵向
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
        mWalletRecyclerView.setLayoutManager(mLinearLayoutManager);
        //设置分隔线
//        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
//        mWalletRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
    }

    private void getAmountCard() {
        denominationList = new ArrayList<>();
        for (int i = 1; i < 7; i++) {
            int den = 20 * i;
            int e = (500 + (100 * (i - 1)));
            int pre = e * den;
            AmountCardModel mAmountCardModel = new AmountCardModel();
            if (i == 6) {
                mAmountCardModel.setDenomination(220 + "W");
                mAmountCardModel.setPresented(getResources().getString(R.string.presented_txt) + 330000);
            } else {
                mAmountCardModel.setDenomination(den + "W");
                mAmountCardModel.setPresented(getResources().getString(R.string.presented_txt) + pre);
            }
            denominationList.add(mAmountCardModel);
        }
        UEDataManager.getInstance().setAmountCardModelList(denominationList);
    }

    private void getWalletData() {
        WalletsModel mWalletsModel1 = new WalletsModel();
        mWalletsModel1.setSelected(true);
        mWalletsModel1.setWalletIcon(R.mipmap.icon_wallet_btc);
        mWalletsModel1.setWalletUnit("BTC");
        mWalletsModel1.setWalletLimit("9.45135461");

        WalletsModel mWalletsModel2 = new WalletsModel();
        mWalletsModel2.setSelected(false);
        mWalletsModel2.setWalletIcon(R.mipmap.icon_wallet_usdt);
        mWalletsModel2.setWalletUnit("USDT");
        mWalletsModel2.setWalletLimit("10.45135462");

        WalletsModel mWalletsModel3 = new WalletsModel();
        mWalletsModel3.setSelected(false);
        mWalletsModel3.setWalletIcon(R.mipmap.icon_wallet_cnv);
        mWalletsModel3.setWalletUnit("CNV");
        mWalletsModel3.setWalletLimit("11.45135463");

        WalletsModel mWalletsModel4 = new WalletsModel();
        mWalletsModel4.setSelected(false);
        mWalletsModel4.setWalletIcon(R.mipmap.icon_wallet_ltc);
        mWalletsModel4.setWalletUnit("LTC");
        mWalletsModel4.setWalletLimit("12.45135464");

        WalletsModel mWalletsModel5 = new WalletsModel();
        mWalletsModel5.setSelected(false);
        mWalletsModel5.setWalletIcon(R.mipmap.icon_wallet_bth);
        mWalletsModel5.setWalletUnit("BCH");
        mWalletsModel5.setWalletLimit("13.45135465");

        WalletsModel mWalletsModel6 = new WalletsModel();
        mWalletsModel6.setSelected(false);
        mWalletsModel6.setWalletIcon(R.mipmap.icon_wallet_eth);
        mWalletsModel6.setWalletUnit("ETH");
        mWalletsModel6.setWalletLimit("14.45135466");

        mWalletsModelList.clear();

        if (isUEWallet) {
            mWalletsModelList.add(mWalletsModel1);
            mWalletsModelList.add(mWalletsModel2);
            mWalletsModelList.add(mWalletsModel3);
        } else {
            mWalletsModelList.add(mWalletsModel1);
            mWalletsModelList.add(mWalletsModel2);
            mWalletsModelList.add(mWalletsModel3);
            mWalletsModelList.add(mWalletsModel4);
            mWalletsModelList.add(mWalletsModel5);
            mWalletsModelList.add(mWalletsModel6);
        }
        UEDataManager.getInstance().setWalletsModelList(mWalletsModelList);
    }

    private void getWalletStripData() {
        WalletStripModel mWalletsModel1 = new WalletStripModel();
        mWalletsModel1.setSelected(true);
        mWalletsModel1.setWalletUnit("BTC");
        mWalletsModel1.setWalletLimit("9.45135461");

        WalletStripModel mWalletsModel2 = new WalletStripModel();
        mWalletsModel2.setSelected(false);
        mWalletsModel2.setWalletUnit("USDT");
        mWalletsModel2.setWalletLimit("10.45135462");

        WalletStripModel mWalletsModel3 = new WalletStripModel();
        mWalletsModel3.setSelected(false);
        mWalletsModel3.setWalletUnit("CNV");
        mWalletsModel3.setWalletLimit("11.45135463");

        WalletStripModel mWalletsModel4 = new WalletStripModel();
        mWalletsModel4.setSelected(false);
        mWalletsModel4.setWalletUnit("LTC");
        mWalletsModel4.setWalletLimit("12.45135464");

        WalletStripModel mWalletsModel5 = new WalletStripModel();
        mWalletsModel5.setSelected(false);
        mWalletsModel5.setWalletUnit("BCH");
        mWalletsModel5.setWalletLimit("13.45135465");

        WalletStripModel mWalletsModel6 = new WalletStripModel();
        mWalletsModel6.setSelected(false);
        mWalletsModel6.setWalletUnit("ETH");
        mWalletsModel6.setWalletLimit("14.45135466");

        mWalletStripModelList.clear();

        if (isUEWallet) {
            mWalletStripModelList.add(mWalletsModel1);
            mWalletStripModelList.add(mWalletsModel2);
            mWalletStripModelList.add(mWalletsModel3);
        } else {
            mWalletStripModelList.add(mWalletsModel1);
            mWalletStripModelList.add(mWalletsModel2);
            mWalletStripModelList.add(mWalletsModel3);
            mWalletStripModelList.add(mWalletsModel4);
            mWalletStripModelList.add(mWalletsModel5);
            mWalletStripModelList.add(mWalletsModel6);
        }
        UEDataManager.getInstance().setWalletStripModelList(mWalletStripModelList);
    }
}
